const { execSql } = require('./db/mysql')

const signup = async (userInfo) => {
  let sql, result
  try {
    sql = 'select username from users where username = ?'
    result = await execSql(sql, [userInfo.username])
    // 如果用户已存在
    if (result.length > 0) {
      return false
    }
    // 如果用户不存在，在创建新用户
    sql = 'insert into users set ?'
    result = await execSql(sql, userInfo)
    return result
  } catch (error) {
    console.log(error)
  }
}

const login = async (userInfo) => {
  let sql = 'select * from users where username = ? and password = ?'
  try {
    const result = await execSql(sql, [userInfo.username, userInfo.password])
    return result
  } catch (error) {
    console.log(error)
  }
}

const changeAvatar = async (userInfo) => {
  let sql = 'update users set avatar = ? where username = ?'
  try {
    const result = await execSql(sql, [userInfo.avatar, userInfo.username])
    return result
  } catch (error) {
    console.log(error)
  }
}

const getUserLikes = async (userInfo) => {
  let sql = 'select message_id from likes where username = ?'
  try {
    const results = await execSql(sql, [userInfo.username])
    return results
  } catch (error) {
    console.log(error)
  }
}

let data = {
  username: 'zhudadi1',
}

getUserLikes(data).then((result) => {
  console.log(result)
})
